<?php
/**
 * 
 * Module helper class file
 * 
 * @author		Stijn Van Minnebruggen
 * @version		1.0.0
 * 
 */

class Module
{
	
	/**
	 * Holds the table row class (odd or even)
	 * 
	 * @var		string
	 */
	
	private $_row_style = 'odd';
	
	
	/**
	 * Table row structure
	 * 
	 * @var		array
	 */
	
	private $_structure;
	
	
	/**
	 * Module settings
	 * 
	 * @var		array
	 */
	
	private $_settings;
	
	
	/**
	 * 
	 * 
	 * @param	string	$var
	 * @return	bool
	 */
	
	public function __construct()
	{
		global $modules;
		$this->_settings = $modules->getSettings();
	}
	
	
	/**
	 * Set table structure
	 * 
	 * @param	array	$structure
	 * @return	void
	 */
	
	public function setStructure($structure)
	{
		$this->_structure = $structure;
		return true;
	}
	
	
	/**
	 * Print table row
	 * 
	 * @todo	Make this function much much much more versatile!!
	 * 
	 * @param	array	$row
	 * @param	string	$extra
	 * @param	bool	$edit
	 * @param	bool	$del
	 * @param	bool	$add
	 * @return	void
	 */
	
	public function printTableRow($row, $extra=false, $edit=true, $del=true, $add=false, $addText=false)
	{
		// required
			global $cosa, $user;
			if(!isset($row['id'])) $cosa->oops("Please provide 'id' in table row.");
		
		// row style
			$this->_row_style = ($this->_row_style == 'odd') ? 'even' : 'odd';
			echo '<tr class="'.$this->_row_style.'">'."\n";
		
		// buttons and actions
			if($add || $edit || $del) {
				echo '<td>';
				if($add) $this->printAddLink($addText, false, $extra);
				if($edit) $this->printEditLink($row['id'], !$user->hasEditPerm(), $extra);
				if($del) $this->printDelLink($row['id'], !$user->hasDelPerm(), false, $extra);
				if(isset($row['icon'])) echo $row['icon'];
				echo '</td>'."\n";
			}
		
		// loop trough structure
			foreach($this->_structure as $k => $v) {
				echo '<td>';
				echo (isset($row[$v])) ? $row[$v] : '-';
				echo '</td>'."\n";
			}
		
		// close table row
			echo '</tr>'."\n";
		
	}
	
	
	/**
	 * Print table row with single cell
	 * 
	 * @param	string	$txt
	 * @return	void
	 */
	
	public function printEmptyTableRow($txt='')
	{
		// get number of columns
			$numCols = 1; // add one for the buttons and actions
			foreach($this->_structure as $k) $numCols++;
		
		// print row
			echo '<tr>'."\n";
			echo '<td colspan="'.$numCols.'">'.$txt.'</td>'."\n";
			echo '</tr>'."\n";
		
	}
	
	
	/**
	 * Print edit link
	 * 
	 * @param	int		$id
	 * @param	bool	$disabled
	 * @return	void
	 */
	
	public function printEditLink($id, $disabled=false, $extra=false)
	{
		global $cosa, $lang, $m, $s;
		$theUrl = $cosa->system('base-url').'/?m='.$m.'&s='.$s.'&edit='.$id;
		if($extra) $theUrl .= $extra;
		$imgUrl = $cosa->system('base-url').'/kernel/files/img/icons/s/edit.gif';
		if(!$disabled) echo '<a href="'.$theUrl.'" class="editLink">';
		echo '<img src="'.$imgUrl.'" alt="'.$lang->get('edit').'" />';
		if(!$disabled) echo '</a>'."\n";
	}
	
	
	/**
	 * Print delete link
	 * 
	 * @param	int		$id
	 * @param	bool	$disabled
	 * @param	string	$showText
	 * @return	void
	 */
	
	public function printDelLink($id, $disabled=false, $text=false, $extra=false)
	{
		global $cosa, $lang, $m, $s;
		$theUrl = $cosa->system('base-url').'/?m='.$m.'&s='.$s.'&del='.$id;
		if($extra) $theUrl .= $extra;
		$imgUrl = $cosa->system('base-url').'/kernel/files/img/icons/s/trash.gif';
		if(!$disabled) echo '<a href="javascript:if(confirm(\''.$lang->get('suredelete').'\')) window.location=\''.$theUrl.'\';" class="delLink">';
		echo '<img src="'.$imgUrl.'" alt="'.$lang->get('del').'" />';
		if($text) echo ' &nbsp;'.$text;
		if(!$disabled) echo '</a>'."\n";
	}
	
	
	/**
	 * Print add link
	 * 
	 * @param	string	$title
	 * @param	bool	$disabled
	 * @return	void
	 */
	
	public function printAddLink($title=false, $disabled=false, $extra=false)
	{
		global $cosa, $lang, $m, $s;
		$theUrl = $cosa->system('base-url').'/?m='.$m.'&s='.$s.'&add';
		if($extra) $theUrl .= $extra;
		if(!$title) $title = $lang->get('add');
		if(!$disabled) echo '<a href="'.$theUrl.'" class="addLink">';
		echo $title;
		if(!$disabled) echo '</a>'."\n";
	}
	
	
	/**
	 * Print sorting link (for a data table)
	 * 
	 * @param	string	$title
	 * @param	string	$table_key
	 * @param	string	$extra
	 * @return	void
	 */
	
	public function printSortLink($title, $table_key, $extra=false)
	{
		// required
			global $cosa, $lang, $m, $s, $sort, $dir;
		
		// direction
			if($sort == $table_key) $newdir = (strtolower($dir) == 'asc') ? 'DESC' : 'ASC';
			else $newdir = 'ASC';
		
		// arrow
			$arrowD = array('ASC' => 'u', 'DESC' => 'd');
			$arrow = $arrowD[$newdir];
		
		// set vars
			$imgUrl = $cosa->system('base-url').'/kernel/files/img/icons/s/arrow_'.$arrow.'.gif';
			$theUrl = $cosa->system('base-url').'/?m='.$m.'&sort='.$table_key.'&dir='.$newdir;
			if($s) $theUrl .= '&s='.$s;
			if($extra) $theUrl .= $extra;
		
		// echo link
			echo '<a href="'.$theUrl.'">'.$title;
			if($sort == $table_key) echo ' <img src="'.$imgUrl.'" alt="'.$newdir.'" />';
			echo '</a>'."\n";
		
	}
	
	
	/**
	 * Query data and add sorting and paging
	 * 
	 * @param	string	$query
	 * @param	bool	$test
	 * @return	reference
	 */
	
	public function queryData($query, $useLang=true, $test=false)
	{
		// required
			global $cosa, $db, $sort, $dir, $limit_f, $limit_t, $lang;
			if(!$limit_f) $limit_f = false;
			if(!$limit_t) $limit_t = false;
		
		// add language
			if($useLang) {
				$query .= (strpos($query, 'WHERE') === false) ? ' WHERE ' : ' AND ';
				$query .= "language_id = '".addslashes($lang->id)."' ";
			}
		
		// add sorting + paging
			if(!strpos('ORDER', $query)) $query .= ' ORDER BY `'.$sort.'` '.$dir;
			if(!strpos('LIMIT', $query) && $limit_f && $limit_t) $query .= ' LIMIT '.$limit_f.', '.$limit_t;
		
		// return query
			if($test) die($query);
			$res = @mysql_query($query) or $cosa->oops($query, 'db');
			return $res;
		
	}
	
	
	/**
	 * Print bool dropdown
	 * 
	 * @param	string	$name
	 * @param	string	$value
	 * @param	string	$onchange
	 * @return	void
	 */
	
	public function printBoolDropdown($name, $value=false, $onchange=false)
	{
		global $lang;
		echo '<select name="'.$name.'"';
		if($onchange) echo ' onchange="'.$onchange.'"';
		echo '>'."\n\t";
		$selected = ($value == 1) ? ' selected="selected"' : '';
		echo '<option value="1"'.$selected.'>'.$lang->get('yes').'</option>';
		$selected = ($value == 0) ? ' selected="selected"' : '';
		echo '<option value="0"'.$selected.'>'.$lang->get('no').'</option>';
		echo '</select>'."\n";
	}
	
	
	/**
	 * Print time input field
	 * 
	 * @param	string		$name
	 * @param	datetime	$value
	 * @return	void
	 */
	
	public function timeInput($name='datepicker', $value=false)
	{
		?>
		<script type="text/javascript">
		$(document).ready(function() {
			var dp = $("#datepicker_<?php echo $name; ?>");
			dp.datepicker({
				firstDay: 1,
				altField: '#<?php echo $name; ?>',
				altFormat: 'yy-mm-dd'
			});
			<?php
			if($value) {
				?>
				var currVal = new Date();
				currVal.setFullYear(<?php echo substr($value, 0, 4); ?>,<?php echo (int)substr($value, 5, 2)-1; ?>,<?php echo (int)substr($value, 8, 2); ?>);
				dp.datepicker('setDate', currVal);
				<?php
			}
			?>
		});
		</script>
		<div id="datepicker_<?php echo $name; ?>"></div>
		<input type="hidden" id="<?php echo $name; ?>" name="<?php echo $name; ?>" value="<?php echo $value; ?>" />
		<?php
	}
	
	
	/**
	 * Check module settings
	 * 
	 * @param	string	$key
	 * @return	string
	 */
	
	public function settings($key)
	{
		if(!isset($this->_settings[$key])) return false;
		return $this->_settings[$key];
	}
	
	
	/**
	 * Save module settings
	 * 
	 * @param	string $var
	 * @return	bool
	 */
	
	public function saveSettings($key, $value)
	{
		global $modules;
		$modules->setSettings($key, $value);
	}
	
	
}


?>